package lab2p2;

import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.event.ChangeListener;

/**
 * <p>Title: AirplaneOrderSystem</p>
 * <p>Description: CS 235 Lab #2</p>
 *         Example of how not using encapsulation can result in bugs.
 * @author Anthony Varghese, inspired by an example program in Head First C#.
 * @date   August 8th 2013
 * @team   
 */
public class AirplaneOrderSystem extends JFrame implements ItemListener, ChangeListener {
	/**
	 * Data members
	 *  the serialVersionUID takes care of a compiler warning.
	 */
	private static final long serialVersionUID = 1L;
	private JSpinner spin;
	private JLabel cost;
	private String COST_STRING_PREFIX = "Total cost: $";
	private JCheckBox luxury;
	private boolean luxury_flag = false;
	private AirplaneCosting costStructure;
	
	
	/**
	 * Default constructor for AirplaneOrderSystem
	 */
	public AirplaneOrderSystem() {
		InitializeWindow();
	}

	/**
	 * Constructor (overloaded) for AirplaneOrderSystem
	 * @param arg0
	 */
	public AirplaneOrderSystem(String arg0) {
		super(arg0);
		InitializeWindow();
	}

	private void InitializeWindow(){
		/*
		 * Set the size of the window
		 */
		setBounds(100, 100, 300, 300);
		
		/*
		 * set up the layout ourselves
		 */
		this.setLayout( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
		
		/*
		 * Set up a spin box and a label next to it.
		 */
		JLabel spinLabel = new JLabel("Number of planes: ");
		spinLabel.setBounds(20, 100, 150, 30 );
		spin = new JSpinner();
		spin.setBounds( 155, 100, 75, 30 );
		add( spin );
		add( spinLabel );
		spin.addChangeListener( this );
		
		/*
		 * Set up a label for total cost
		 */
		cost = new JLabel(COST_STRING_PREFIX + "0.00");
		cost.setBounds(50, 50, 250, 40);
		add( cost );
		
		/*
		 * Set up an option for Luxury Jet
		 */
		luxury = new JCheckBox("Luxury");
		luxury.setBounds(20, 200, 150, 30 );
		luxury.setSelected( luxury_flag );
		add( luxury );
		luxury.addItemListener( this );

		costStructure = new AirplaneCosting();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AirplaneOrderSystem window = new AirplaneOrderSystem("Order your aiplanes here!");
		window.setVisible( true );
	}

	/**
	 * stateChanged - gets called when the spin box changes
	 * @param arg0
	 */
	public void stateChanged(ChangeEvent arg0) {
		if (arg0.getSource() == spin){
			int n = (Integer) (spin.getValue());
			costStructure.setNumber( n );
			setTotal();
		}
	}

	/**
	 * itemStateChanged is triggered by a user clicking on the Luxury box
	 */
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == luxury){
			luxury_flag = ! luxury_flag;
			costStructure.luxury = luxury_flag;
			setTotal();
		}
	}


	private void setTotal() {
		float amount = costStructure.getCost();
		cost.setText( COST_STRING_PREFIX + String.format(" %.2f ", amount) );
	}
}
